package com.psmz.iwi.docanalyzer.core.document.entities;

import java.util.HashMap;
import java.util.Map;

import com.psmz.iwi.docanalyzer.core.document.parser.Parser;
import com.psmz.iwi.docanalyzer.core.document.parser.pdf.PDFParser;
import com.psmz.iwi.docanalyzer.core.document.parser.word.WordParser;

/**
 * Obslugiwany typ dokumentu. Aktualnie obslugiwane PDF i WORD.
 * 
 * @author piotrs10
 * 
 */
public enum DocumentType {
	PDF("PDF", new String[] { "pdf" }), WORD("WORD", new String[] { "doc",
			"docx" });

	private static Map<String, DocumentType> codesMap;
	private static Map<String, DocumentType> extensionMap;
	private String code;
	private String[] supportedFileExtensions;

	private DocumentType(String code, String[] supportedFileExtensions) {
		this.code = code;
		this.supportedFileExtensions = supportedFileExtensions;
	}

	public String getCode() {
		return code;
	}

	public String[] getSupportedFileExtensions() {
		return supportedFileExtensions;
	}

	public Parser getParser() {
		Parser parser = null;
		switch (this) {
		case PDF:
			parser = new PDFParser();
			break;
		case WORD:
			parser = new WordParser();
			break;
		default:
			parser = null;
			break;

		}
		return parser;
	}

	static {
		codesMap = new HashMap<String, DocumentType>();
		extensionMap = new HashMap<String, DocumentType>();
		DocumentType[] values = DocumentType.values();
		for (DocumentType value : values) {
			codesMap.put(value.getCode(), value);
			for (String extension : value.getSupportedFileExtensions()) {
				extensionMap.put(extension, value);
			}
		}
	}

	public static DocumentType getByCode(String code) {
		return codesMap.get(code);
	}

	public static DocumentType getByExtension(String extension) {
		return extensionMap.get(extension);
	}

}
